Skip to content

Improve rand() initializer annotations#77

Open
realanu0812 wants to merge 4 commits intobrian-team:masterfrom
realanu0812:rand-annotation-initializers
Open

Improve rand() initializer annotations#77
realanu0812 wants to merge 4 commits intobrian-team:masterfrom
realanu0812:rand-annotation-initializers

Conversation

@realanu0812
Copy link
Copy Markdown

This PR improves handling of initializer expressions involving rand() in markdown export.

It adds a small SymPy-based annotation step for expressions containing a single rand() call, inferring lower and upper bounds via substitution and displaying them as a Uniform range.

Additionally, it fixes an issue with handling array-based indices in expand_initializer, avoiding ambiguous truth-value errors.

The scope is intentionally kept small and focused on initializer expressions.

@mstimberg
Copy link
Copy Markdown
Member

Hi @realanu0812 thank you for the PR, and apologies for taking so long to reply. Since you opened it, the master branch has moved on (and the test suite is not failing any more) – could you please merge the latest master into your branch and resolve the conflict?

@realanu0812
Copy link
Copy Markdown
Author

Hi @mstimberg,

I’ve merged the latest master into my branch, resolved the conflicts, and updated the PR. I also ran the tests locally to ensure everything is working as expected.

Please let me know if anything else needs adjustment.

Best,
Anurag

@mstimberg
Copy link
Copy Markdown
Member

I also ran the tests locally to ensure everything is working as expected.

Could you please give a simple example that shows that it is working?

@realanu0812
Copy link
Copy Markdown
Author

Hi @mstimberg,

Thanks for pointing this out. I again checked it with a minimal example, I realized the annotation was not reducing correctly to the intended lower/upper bounds, so I fixed that in the new PR before sharing the example.

It is now working as intended for the supported case of a single rand() call.
Example:

from brian2 import *
from brian2tools import *
from brian2.devices.device import get_device

set_device('markdown')
start_scope()

Vr = -60*mV
Vt = -50*mV

G = NeuronGroup(5, 'v : volt')
G.v = 'Vr + rand() * (Vt - Vr)'

run(0*ms)

print(get_device().md_text)

Relevant exported output:
* Variable $v$= $Vr + \left(- Vr + Vt\right) \cdot \mathcal{U}{\left(0, 1\right)}$ (approximately Uniform($Vr$, $Vt$))

The annotation is inferred by substituting rand() with 0 and 1 to compute the lower and upper bounds.
Please let me know if you’d prefer a different format or placement for the annotation.

Best,
Anurag

@realanu0812
Copy link
Copy Markdown
Author

Hi @mstimberg,

This issue turned out to be a very useful entry point for me into an area closely related to my GSoC proposal — exporter behavior, representation of initialization/state, and the broader serialization/deserialization workflow. While this PR is only a small scoped improvement, working on it helped me understand the codebase and some of the design questions around model representation much better.

I’ve submitted my GSoC proposal for the Brian serialization/deserialization project, and if selected, I’d be very happy to continue contributing in this area more broadly.

Best,
Anurag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants